var app = getApp()
const baseUrl = app.globalData.baseUrl;
Page({
data:{      
  hasList: true,          // 列表是否有数据
  totalPrice: 0,           // 总价，初始为0
  numcount: 0,
  selectAllStatus: false   , // 全选状态，默认全选
  buyProuductId:[] ,//选中商品的id列表
  cartList:[]
},
onLoad(){
  
}

 , 
 //去支付
  payfor: function (e) {
    var isChecked = false;
    for (var i = 0; i < this.data.cartList.length; i++) {
      if (this.data.cartList[i].selected) {
        isChecked = true;
      }
    }
    if (!isChecked) {
      wx.showToast({
        title: '请选择至少一件商品',
        icon: 'none'
      })
      return;
    }

    var selectedItems = new Array;
    for (var i = 0; i < this.data.cartList.length; i++) {
      if (this.data.cartList[i].selected) {
        selectedItems.push(this.data.cartList[i]);
      }
    }
    console.log(JSON.stringify(selectedItems));
    wx.setStorageSync("cart", JSON.stringify(selectedItems));
    // JSON.parse(wx.getStorageSync("cart"))
    wx.navigateTo({
      url: '/pages/payfor/payfor'
    })
  },
  checkAll: function (e) {
    var checkAll = !e.currentTarget.dataset.id;
    console.log("checkall",checkAll)  
  }
  ,onShow:function(){
    var that = this;
    var url = baseUrl + '/yq/cart/list';
    var params = {
    };
    var success = res => {
      that.setData({
        cartList: res.info,
      })
    //  console.log("购物车列表：", res);
    }
    var fail = res => {
    }
    app.request(url, params, success, fail);
    this.setData({        //默认不全选
      cartList: this.data.cartList,
      selectAllStatus:false,
      totalPrice: 0
    })
  
  }
  ,
  selectList(e) {
    let Allprice = 0;
    const index = e.currentTarget.dataset.index;    // 获取data- 传进来的index
    let cartList = this.data.cartList;                    // 获取购物车列表
    const selected = cartList[index].selected;         // 获取当前商品的选中状态
    cartList[index].selected = !selected;            // 改变状态
    this.setData({
      cartList: cartList
    });
    this.getTotalPrice();                           // 重新获取总价
  
    //是否全选判断
    for (let i = 0; i < this.data.cartList.length;i++) {
      Allprice += cartList[i].count * cartList[i].formatPrice; 
    }
    if (Allprice == this.data.totalPrice) {
      this.data.selectAllStatus = true;
    }
    else {
      this.data.selectAllStatus = false;
    }
    this.setData({
      cartList: this.data.cartList,
      selectAllStatus: this.data.selectAllStatus,
    })
  },
  getTotalPrice() {
    let cartList = this.data.cartList;                  // 获取购物车列表
    let total = 0;
    let numcount=0;
    for (let i = 0; i < cartList.length; i++) {         // 循环列表得到每个数据
      if (cartList[i].selected) {                   // 判断选中才会计算价格
        total += cartList[i].count * cartList[i].formatPrice;     // 所有价格加起来
        numcount += Number(cartList[i].num);
      }
    }
    this.setData({                                // 最后赋值到data中渲染到页面
      cartList: cartList,
      totalPrice: total,
      numcount:numcount
    });
  }
  ,
  selectAll(e) {
    let selectAllStatus = this.data.selectAllStatus;    // 是否全选状态
    selectAllStatus = !selectAllStatus;
    let cartList = this.data.cartList;

    for (let i = 0; i < cartList.length; i++) {
      cartList[i].selected = selectAllStatus;            // 改变所有商品状态
    }
    this.setData({
      selectAllStatus: selectAllStatus,
      cartList: cartList
    });
    this.getTotalPrice();               // 重新获取总价
  }

})